package com.wbao.dianniu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wbao.dianniu.R;
import com.wbao.dianniu.customView.SpanTextView;
import com.wbao.dianniu.data.QuestAnswerCountsData;
import com.wbao.dianniu.data.QuestAnswerListData;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.AnswerPraiseManager;
import com.wbao.dianniu.utils.CommentLongClickHelper;
import com.wbao.dianniu.utils.PictureDownloadUtils;
import com.wbao.dianniu.utils.ReportType;
import com.wbao.dianniu.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 电纽提问-查看问答
 *
 * @author yejianjun
 */
public class QuestAnswerAdapter extends BaseAdapter {
    private Context mContext;
    private List<QuestAnswerListData> mList;

    public QuestAnswerAdapter(Context context) {
        mContext = context;
    }

    public void addData(List<QuestAnswerListData> list, QuestAnswerCountsData qacd) {
        if (null == mList) {
            mList = new ArrayList<QuestAnswerListData>();
        }
        for (int i = 0; i < list.size(); i++) {
            mList.add(list.get(i));
        }
    }

    public List<QuestAnswerListData> getList() {
        return mList;
    }

    public void addOneData(QuestAnswerListData data) {
        if (null != mList) {
            mList.add(0, data);
        }
    }

    public void deleteOneData(int position) {
        mList.remove(position);
        notifyDataSetChanged();
    }

    public void deleteById(int id) {
        if (null == mList)
            return;
        for (int i = 0; i < mList.size(); i++) {
            if (id == mList.get(i).id) {
                mList.remove(i);
                break;
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (null == mList)
            return 0;
        else
            return mList.size();
    }

    public void setComment(String userName) {


    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if (null == convertView) {
            LayoutInflater mInflater = LayoutInflater.from(mContext);
            convertView = mInflater.inflate(R.layout.quest_answer_list_item, null);
            holder = new Holder();
            holder.head = (ImageView) convertView.findViewById(R.id.user_head);
            holder.name = (TextView) convertView.findViewById(R.id.user_name);
            holder.label = (TextView) convertView.findViewById(R.id.user_label);
            holder.sexIV = (ImageView) convertView.findViewById(R.id.user_sex);
            holder.addV = (ImageView) convertView.findViewById(R.id.add_v_iv);
            holder.goodCounts = (TextView) convertView.findViewById(R.id.quest_answer_praise_tv);
            holder.content = (SpanTextView) convertView.findViewById(R.id.quest_answer_content);
            holder.qaRelative = (RelativeLayout) convertView.findViewById(R.id.qa_answer_item_relative);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        if (null == mList || mList.size() < position) {
            return convertView;
        }
        QuestAnswerListData data = mList.get(position);
        if (null != data) {
            PictureDownloadUtils.displayHeadImage(mContext, holder.head, data.headPic);
            holder.name.setText(data.realName == null ? "" : data.realName);
            holder.label.setText(data.label == null ? "" : data.label);
            if(null == data.userInfo){
                Utils.showSex(mContext,null,holder.sexIV);
            }else{
                Utils.showSex(mContext, data.userInfo.sex, holder.sexIV);
            }
            Utils.showAuth(data.userInfo,holder.addV);

            Utils.praiseChanged(mContext,data.isGood,data.goodCount,holder.goodCounts);
            Utils.showSpanContentComment(mContext, data.content, data.noticeJson, holder.content, true);
        }
        //点击事件
        OnClickListener headListener = new clickListener(data);
        holder.head.setOnClickListener(headListener);
        holder.content.setOnClickListener(headListener);
        holder.qaRelative.setOnClickListener(headListener);
        holder.goodCounts.setOnClickListener(new praiseListener(position, data));
//        holder.content.setOnLongClickListener(new longClickListener(data));
        return convertView;
    }

    /*class longClickListener implements View.OnLongClickListener {
        private QuestAnswerListData data;

        public longClickListener(QuestAnswerListData data) {
            this.data = data;
        }

        @Override
        public boolean onLongClick(View v) {

            CommentLongClickHelper.getInstance().commentLongClick(mContext, data, ReportType.TYPE_REPLY);
            return false;
        }
    }*/


	/*class LongClickListener implements OnLongClickListener{

		@Override
		public boolean onLongClick(View v) {
			if (v.getId() == R.id.quest_answer_head || v.getId() == R.id.quest_answer_name || v.getId() == R.id.quest_answer_label) {
				return true;
			}
			return false;
		}
		
	}*/

    /**
     * 点击功能
     */
    class clickListener implements OnClickListener {
        private QuestAnswerListData data;

        public clickListener(QuestAnswerListData data) {
            this.data = data;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.user_head:
                    Utils.intoHeadInfoActivity(data.userInfo.accountId, mContext);
                    break;
                case R.id.quest_answer_content:
                case R.id.qa_answer_item_relative:
                case R.id.user_head_layout:
                    CommentLongClickHelper commentLongClickHelper = CommentLongClickHelper.getInstance();
                    commentLongClickHelper.commentLongClick(mContext, data, ReportType.TYPE_REPLY,data.realName);
                    /*if (data.accountId != GlobalContext.getAccountId()) {
                        ReplyHelper.getInstance().reply(data.realName, data.accountId);
                    }*/

                    break;
                default:
                    break;
            }

        }

    }

    class praiseListener implements OnClickListener {
        private int position;
        private QuestAnswerListData data;

        public praiseListener(int position, QuestAnswerListData data) {
            this.position = position;
            this.data = data;
        }

        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.quest_answer_praise_tv) {
                if (1 == data.isGood) {
                    data.goodCount--;
                    data.isGood = 0;
                } else {
                    data.goodCount++;
                    data.isGood = 1;
                }
                notifyUI(position, data);
                //通知后台更新数据
                AnswerPraiseManager manager = new AnswerPraiseManager(mContext);
                manager.answerPraise(GlobalContext.getAccountId(), data.questId, data.id);
            }

        }

    }

    private void notifyUI(int position, QuestAnswerListData data) {
        if (null != data && null != mList) {
            mList.set(position, data);
        }
        notifyDataSetChanged();
    }

    private class Holder {
        public ImageView head;
        public TextView name;
        public TextView label;
        public ImageView sexIV;
        public ImageView addV;
//        public ImageView isGood;
        public TextView goodCounts;
        public SpanTextView content;
        public RelativeLayout qaRelative;
    }

}
